package com.eee.machine.configuration;

import com.eee.machine.beans.TUser;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.util.Strings;

import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

public class LoginFilter implements Filter {

    private String[] excludeUrl = new String[]{"/admin/login","/admin/adminLogin","*.js","*.css","*.jpg","*.jpeg","*.png"};

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        String uri = request.getRequestURI();
//        System.out.println("uri=="+uri);

        //判断是否为ajax请求，默认不是
        boolean isAjaxRequest = false;
        if(!Strings.isBlank(request.getHeader("x-requested-with")) && request.getHeader("x-requested-with").equals("XMLHttpRequest")){
            isAjaxRequest = true;
        }

//        SysUser sysUser = sessionContext.getSysUserFromSession(req);
//        if (sysUser != null && sysUser.getUserId() != null) {
//            chain.doFilter(req, res);
//        }else{//Session用户为空，登录过期
//            if(isAjaxRequest){// 如果是ajax请求，则不是跳转页面，使用response返回结果
//                res.setHeader("noAuthentication", "true");
//                ResultWithObject resultWithObject = new ResultWithObject(CC.NEGATIVE_1, CC.RESULT_MESSAGE_TEXT_DEFAULT);
//                resultWithObject.setMsg("登录已失效，请刷新页面或重新登录！");
//                res.setContentType("application/json;charset=UTF-8");
//                PrintWriter writer = res.getWriter();
//                writer.write(JasonUtils.Object2String(resultWithObject));
//                writer.close();
//                res.flushBuffer();
//            }else{
//                res.sendRedirect("http://xxxx.com/loginUI");
//            }
//        }

        if(uri.contains("/admin/adminLogin") && !isAjaxRequest){
            response.sendRedirect("/admin/login");
        }
        if(ArrayUtils.contains(excludeUrl, uri) || uri.contains(".")){
            filterChain.doFilter(servletRequest,servletResponse);
//        }
//        if(ArrayUtils.contains(excludeUrl, uri) || uri.endsWith(".js") || uri.endsWith(".css") || uri.endsWith(".jpg")
//        || uri.endsWith(".png") || uri.endsWith(".ico") ||uri.endsWith(".jpeg") || uri.endsWith(".gif")){
//            filterChain.doFilter(servletRequest,servletResponse);
        }else{
            Object loginUser = session.getAttribute("loginUser");
            if(loginUser == null){
                response.sendRedirect("/admin/login");
            }else{
                filterChain.doFilter(servletRequest,servletResponse);
            }
        }

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}
